home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML ConsoleMax.sea / XML ConsoleMax / Required / swingall.jar / javax / swing / text / rtf / RTFParser.class (.txt) < prev    next >
Encoding:
Java Class File  |  1999-07-15  |  4.7 KB  |  251 lines

  1. package javax.swing.text.rtf;
  2.  
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.IOException;
  5. import java.io.PrintStream;
  6.  
  7. abstract class RTFParser extends AbstractFilter {
  8.    public int level = 0;
  9.    private int state = 0;
  10.    private StringBuffer currentCharacters = new StringBuffer();
  11.    private String pendingKeyword = null;
  12.    private int pendingCharacter;
  13.    private long binaryBytesLeft;
  14.    ByteArrayOutputStream binaryBuf;
  15.    private boolean[] savedSpecials;
  16.    protected PrintStream warnings;
  17.    private final int S_text = 0;
  18.    private final int S_backslashed = 1;
  19.    private final int S_token = 2;
  20.    private final int S_parameter = 3;
  21.    private final int S_aftertick = 4;
  22.    private final int S_aftertickc = 5;
  23.    private final int S_inblob = 6;
  24.    static final boolean[] rtfSpecialsTable;
  25.  
  26.    static {
  27.       rtfSpecialsTable = (boolean[])AbstractFilter.noSpecialsTable.clone();
  28.       rtfSpecialsTable[10] = true;
  29.       rtfSpecialsTable[13] = true;
  30.       rtfSpecialsTable[123] = true;
  31.       rtfSpecialsTable[125] = true;
  32.       rtfSpecialsTable[92] = true;
  33.    }
  34.  
  35.    public RTFParser() {
  36.       super.specialsTable = rtfSpecialsTable;
  37.    }
  38.  
  39.    public abstract void begingroup();
  40.  
  41.    public void close() throws IOException {
  42.       this.flush();
  43.       if (this.state != 0 || this.level > 0) {
  44.          this.warning("Truncated RTF file.");
  45.  
  46.          while(this.level > 0) {
  47.             this.endgroup();
  48.             --this.level;
  49.          }
  50.       }
  51.  
  52.       super.close();
  53.    }
  54.  
  55.    public abstract void endgroup();
  56.  
  57.    public void flush() throws IOException {
  58.       super.flush();
  59.       if (this.state == 0 && this.currentCharacters.length() > 0) {
  60.          this.handleText(this.currentCharacters.toString());
  61.          this.currentCharacters = new StringBuffer();
  62.       }
  63.  
  64.    }
  65.  
  66.    public abstract void handleBinaryBlob(byte[] var1);
  67.  
  68.    public abstract boolean handleKeyword(String var1);
  69.  
  70.    public abstract boolean handleKeyword(String var1, int var2);
  71.  
  72.    public void handleText(char var1) {
  73.       this.handleText(String.valueOf(var1));
  74.    }
  75.  
  76.    public abstract void handleText(String var1);
  77.  
  78.    protected void warning(String var1) {
  79.       if (this.warnings != null) {
  80.          this.warnings.println(var1);
  81.       }
  82.  
  83.    }
  84.  
  85.    public void write(char var1) throws IOException {
  86.       switch (this.state) {
  87.          case 0:
  88.             if (var1 != '\n' && var1 != '\r') {
  89.                if (var1 == '{') {
  90.                   if (this.currentCharacters.length() > 0) {
  91.                      this.handleText(this.currentCharacters.toString());
  92.                      this.currentCharacters = new StringBuffer();
  93.                   }
  94.  
  95.                   ++this.level;
  96.                   this.begingroup();
  97.                } else if (var1 == '}') {
  98.                   if (this.currentCharacters.length() > 0) {
  99.                      this.handleText(this.currentCharacters.toString());
  100.                      this.currentCharacters = new StringBuffer();
  101.                   }
  102.  
  103.                   if (this.level == 0) {
  104.                      throw new IOException("Too many close-groups in RTF text");
  105.                   }
  106.  
  107.                   this.endgroup();
  108.                   --this.level;
  109.                } else if (var1 == '\\') {
  110.                   if (this.currentCharacters.length() > 0) {
  111.                      this.handleText(this.currentCharacters.toString());
  112.                      this.currentCharacters = new StringBuffer();
  113.                   }
  114.  
  115.                   this.state = 1;
  116.                } else {
  117.                   this.currentCharacters.append(var1);
  118.                }
  119.             }
  120.             break;
  121.          case 1:
  122.             if (var1 == '\'') {
  123.                this.state = 4;
  124.                break;
  125.             } else if (!Character.isLetter(var1)) {
  126.                char[] var8 = new char[]{var1};
  127.                if (!this.handleKeyword(new String(var8))) {
  128.                   this.warning("Unknown keyword: " + var8 + " (" + (byte)var1 + ")");
  129.                }
  130.  
  131.                this.state = 0;
  132.                this.pendingKeyword = null;
  133.                break;
  134.             } else {
  135.                this.state = 2;
  136.             }
  137.          case 2:
  138.             if (Character.isLetter(var1)) {
  139.                this.currentCharacters.append(var1);
  140.             } else {
  141.                this.pendingKeyword = this.currentCharacters.toString();
  142.                this.currentCharacters = new StringBuffer();
  143.                if (!Character.isDigit(var1) && var1 != '-') {
  144.                   boolean var6 = this.handleKeyword(this.pendingKeyword);
  145.                   if (!var6) {
  146.                      this.warning("Unknown keyword: " + this.pendingKeyword);
  147.                   }
  148.  
  149.                   this.pendingKeyword = null;
  150.                   this.state = 0;
  151.                   if (!Character.isWhitespace(var1)) {
  152.                      this.write(var1);
  153.                   }
  154.                } else {
  155.                   this.state = 3;
  156.                   this.currentCharacters.append(var1);
  157.                }
  158.             }
  159.             break;
  160.          case 3:
  161.             if (Character.isDigit(var1)) {
  162.                this.currentCharacters.append(var1);
  163.             } else if (this.pendingKeyword.equals("bin")) {
  164.                long var3 = Long.parseLong(this.currentCharacters.toString());
  165.                this.pendingKeyword = null;
  166.                this.state = 6;
  167.                this.binaryBytesLeft = var3;
  168.                if (this.binaryBytesLeft > 2147483647L) {
  169.                   this.binaryBuf = new ByteArrayOutputStream(Integer.MAX_VALUE);
  170.                } else {
  171.                   this.binaryBuf = new ByteArrayOutputStream((int)this.binaryBytesLeft);
  172.                }
  173.  
  174.                this.savedSpecials = super.specialsTable;
  175.                super.specialsTable = AbstractFilter.allSpecialsTable;
  176.             } else {
  177.                int var7 = Integer.parseInt(this.currentCharacters.toString());
  178.                boolean var2 = this.handleKeyword(this.pendingKeyword, var7);
  179.                if (!var2) {
  180.                   this.warning("Unknown keyword: " + this.pendingKeyword + " (param " + this.currentCharacters + ")");
  181.                }
  182.  
  183.                this.pendingKeyword = null;
  184.                this.currentCharacters = new StringBuffer();
  185.                this.state = 0;
  186.                if (!Character.isWhitespace(var1)) {
  187.                   this.write(var1);
  188.                }
  189.             }
  190.             break;
  191.          case 4:
  192.             if (Character.digit(var1, 16) == -1) {
  193.                this.state = 0;
  194.             } else {
  195.                this.pendingCharacter = Character.digit(var1, 16);
  196.                this.state = 5;
  197.             }
  198.             break;
  199.          case 5:
  200.             this.state = 0;
  201.             if (Character.digit(var1, 16) != -1) {
  202.                this.pendingCharacter = this.pendingCharacter * 16 + Character.digit(var1, 16);
  203.                var1 = super.translationTable[this.pendingCharacter];
  204.                if (var1 != 0) {
  205.                   this.handleText(var1);
  206.                }
  207.             }
  208.             break;
  209.          case 6:
  210.             this.binaryBuf.write(var1);
  211.             --this.binaryBytesLeft;
  212.             if (this.binaryBytesLeft == 0L) {
  213.                this.state = 0;
  214.                super.specialsTable = this.savedSpecials;
  215.                this.savedSpecials = null;
  216.                this.handleBinaryBlob(this.binaryBuf.toByteArray());
  217.                this.binaryBuf = null;
  218.             }
  219.       }
  220.  
  221.    }
  222.  
  223.    public void write(String var1) throws IOException {
  224.       if (this.state != 0) {
  225.          int var2 = 0;
  226.  
  227.          int var3;
  228.          for(var3 = var1.length(); var2 < var3 && this.state != 0; ++var2) {
  229.             this.write(var1.charAt(var2));
  230.          }
  231.  
  232.          if (var2 >= var3) {
  233.             return;
  234.          }
  235.  
  236.          var1 = var1.substring(var2);
  237.       }
  238.  
  239.       if (this.currentCharacters.length() > 0) {
  240.          this.currentCharacters.append(var1);
  241.       } else {
  242.          this.handleText(var1);
  243.       }
  244.  
  245.    }
  246.  
  247.    public void writeSpecial(int var1) throws IOException {
  248.       this.write((char)var1);
  249.    }
  250. }
  251.